#include "MainScreen.h"
#include "MAHeaders.h"
#include "LRESIndex.h"
#include "../std.h"
#include "../Utilities/trim.h"
#include "../Widgets/UIBuilder.h"
#include "../Widgets/MenuItem.h"
#include "../ScreenTransition.h"

MainScreen::MainScreen() {
	mLayout = createMainLayout("LuckyZone", getStringNotIterative(L_SELECT),
			getStringNotIterative(L_EXIT));
	mItems = (ListBox*) mLayout->getChildren()[1];
	mSoftKeys = (Widget*) mLayout->getChildren()[2];

	MenuItem* item;
	String temp;
	int startIcon = RES_ICONS_START;
	for (int i = MAIN_TEXT_START; i != MAIN_TEXT_END + 1; i++) {
		temp.clear();
		item = new MenuItem(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM,
				NULL);
		item->setTitleFont(APPCONTROLLER->getFont("default"));
		temp = getStringIterative(i);
		item->setTitle(temp);
		item->setNextImage(startIcon);
		item->setSkin(APPCONTROLLER->getSkin("default"));
		//item->addWidgetListener(this);
		mItems->add(item);
		startIcon++;
	}

	mItems->setSelectedIndex(0);
	mItems->getChildren()[0]->setSelected(true);

	this->setMain(mLayout);
}

MainScreen::~MainScreen() {
	delete mLayout;
}

void MainScreen::keyPressEvent(int keyCode, int nativeCode) {
	switch (keyCode) {
	case MAK_5:{
		MenuScreen* mscr = (MenuScreen*) APPCONTROLLER->getScreen("MenuScreen");
		mscr->setListener(this);
		mscr->clearMenuItem();
		String t = getStringIterative(L_SERVICE);
		mscr->addMenuItem(t);
		t = getStringIterative(L_ANALYSIS);
		mscr->addMenuItem(t);
		t = getStringIterative(L_EXIT);
		mscr->addMenuItem(t);
		mscr->show();
	}
		break;
	case MAK_UP:
		mItems->selectPreviousItem();
		break;
	case MAK_DOWN:
		mItems->selectNextItem();
		break;
	case MAK_FIRE:
	case MAK_SOFTLEFT:
		int i = mItems->getSelectedIndex();
		if (i == 0 || i == 1) {
			LRESIndex* screen = (LRESIndex*) APPCONTROLLER->getScreen("LRESIndex");
			screen->setIsToday(1 - i);
			APPCONTROLLER->show("LRESIndex");
		} else {
			APPCONTROLLER->show(mItems->getSelectedIndex());
		}
		break;
	case MAK_SOFTRIGHT: {
		MessageBox* msg = (MessageBox*) APPCONTROLLER->getScreen("MessageBox");
		msg->setListener(this);
		String t = "";
		int transfer = APPCONTROLLER->getDataTransfer() / 1024;
		t = getStringIterative(L_MSG_TRANSFER) + integerToString(transfer) + " Kb. " + getStringIterative(L_MSG_EXIT);
		msg->setTitle(t);
		msg->show();
	}
		break;
	}
}

void MainScreen::pointerPressEvent(MAPoint2d point) {
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)) {
		for (int i = 0; i < mItems->getChildren().size(); i++) {
			if (mItems->getChildren()[i]->contains(p)) {
				int index = mItems->getSelectedIndex();
				if (index == i)
					keyPressEvent(MAK_FIRE, 0);
				else
					mItems->setSelectedIndex(i);
				break;
			}
		}
	} else if (mSoftKeys->contains(p)) {
		if (mSoftKeys->getChildren()[0]->contains(p))
			keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftKeys->getChildren()[1]->contains(p))
			keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void MainScreen::pointerReleaseEvent(MAPoint2d point) {

}

void MainScreen::actionIndex(int index) {
	if (index == MAK_SOFTLEFT) {
		APPCONTROLLER->close();
	} else {
		this->show();
	}
}

void MainScreen::menuIndex(int index){
	lprintfln("%d", index);
	this->show();
}
